<?php


namespace addons\official\controller\api;

use addons\official\model\OfficialListForm;
use addons\official\model\OfficialListOnlineMessage;
use addons\official\model\OfficialListTable;
use think\Request;
use think\Validate;

/**
 * 表单提交
 * @package addons\official\controller\api
 */
class Table extends BaseApi
{
    protected $model;
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';



    public function __construct(Request $request = null)
    {
        $this->model = new OfficialListTable();
        parent::__construct($request);
    }

    /**
     * 招聘表单提交
     * @ApiMethod   (POST)
     * @ApiParams   (name="official_list_table_id", type="integer", required=ture, description="职位id")
     * @ApiParams   (name="name", type="string", required=ture, description="应聘人")
     * @ApiParams   (name="gender", type="string", required=ture, description="性别:0=女,1=男")
     * @ApiParams   (name="birthday", type="string", required=ture, description="出生日期")
     * @ApiParams   (name="nationality", type="string", required=ture, description="民族")
     * @ApiParams   (name="politics_status", type="string", required=ture, description="政治面貌")
     * @ApiParams   (name="education", type="string", required=ture, description="最高学历")
     * @ApiParams   (name="native_place", type="string", required=ture, description="籍贯")
     * @ApiParams   (name="school", type="string", required=ture, description="毕业学院")
     * @ApiParams   (name="discipline", type="string", required=ture, description="所学专业")
     * @ApiParams   (name="phone", type="string", required=ture, description="联系电话")
     * @ApiParams   (name="email", type="string", required=ture, description="电子邮件")
     * @ApiParams   (name="address", type="string", required=ture, description="地址")
     * @ApiParams   (name="treatment", type="string", required=ture, description="待遇要求")
     * @ApiParams   (name="detail_requirement", type="string", required=ture, description="详细要求")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询成功",
        "time": "1634823263",
        "data": ""
     })
     */
    public function SetForm(){
        $param = input('param.');
        $official_list_table = $this->model->get($param['official_list_table_id']);
        if (empty($official_list_table)) {
            return json_encode(['code' => 0, 'msg' => __api('table', 'The recruitment information could not be found')]);
        }

        $rule = [
            "official_list_table_id" => 'require',
            "name" => 'require',
            "gender" => 'require',
            "birthday" => 'require',
            "nationality" => 'require',
            "politics_status" => 'require',
            "education" => 'require',
            "native_place" => 'require',
            "school" => 'require',
            "discipline" => 'require',
            "phone" => 'require',
            "email" => 'require|email',
            "address" => 'require',
            "treatment" => 'require',
            "detail_requirement" => 'require',
        ];
        $msg = [
            "official_list_table_id.require" => __api('table', 'Official_list_table_id') . __api('index', 'No_empty'),
            "name.require" => __api('table', 'Name') . __api('index', 'No_empty'),
            "gender.require" => __api('table', 'Gender') . __api('index', 'No_empty'),
            "birthday.require" => __api('table', 'Birthday') . __api('index', 'No_empty'),
            "nationality.require" => __api('table', 'Nationality') . __api('index', 'No_empty'),
            "politics_status.require" => __api('table', 'Politics_status') . __api('index', 'No_empty'),
            "education.require" => __api('table', 'Education') . __api('index', 'No_empty'),
            "native_place.require" => __api('table', 'Native_place') . __api('index', 'No_empty'),
            "school.require" => __api('table', 'School') . __api('index', 'No_empty'),
            "discipline.require" => __api('table', 'Discipline') . __api('index', 'No_empty'),
            "phone.require" => __api('table', 'Phone') . __api('index', 'No_empty'),
            "email.require" => __api('table', 'Email') . __api('index', 'No_empty'),
            "email.email" => __api('table', 'Incorrect email address'),
            "address.require" => __api('table', 'Address') . __api('index', 'No_empty'),
            "treatment.require" => __api('table', 'Treatment') . __api('index', 'No_empty'),
            "detail_requirement.require" => __api('Table', 'detail_requirement') . __api('index', 'No_empty'),
        ];
        $data = [
            "official_list_table_id" => $param['official_list_table_id'],
            "name" => $param['name'],
            "gender" => $param['gender'],
            "birthday" => $param['birthday'],
            "nationality" => $param['nationality'],
            "politics_status" => $param['politics_status'],
            "education" => $param['education'],
            "native_place" => $param['native_place'],
            "school" => $param['school'],
            "discipline" => $param['discipline'],
            "phone" => $param['phone'],
            "email" => $param['email'],
            "address" => $param['address'],
            "treatment" => $param['treatment'],
            "detail_requirement" => $param['detail_requirement'],
        ];
        //验证规则
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            return json_encode(['code' => 0, 'msg' => $validate->getError()]);
        }
        //增加数据
        $data['admin_id'] = $official_list_table['admin_id'];
        $official_list_form = (new OfficialListForm())->create($data);
        if ($official_list_form) {
            return json_encode(['code' => 1, 'msg' => __api('index', 'Submit successfully')]);
        }
    }

    /**
     * 在线留言
     * @ApiMethod   (POST)
     * @ApiParams   (name="official_category_id", type="integer", required=ture, description="职位id")
     * @ApiParams   (name="title", type="string", required=ture, description="标题")
     * @ApiParams   (name="name", type="string", required=ture, description="姓名")
     * @ApiParams   (name="phone", type="string", required=ture, description="手机号")
     * @ApiParams   (name="content", type="string", required=ture, description="详情")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "设置成功",
    "time": "1634823263",
    "data": ""
    })
     */
    public function SetOnlineMessageForm(){
        $param = input('param.');
        $rule = [
            "official_category_id" => 'require',
            "name" => 'require',
            "phone" => 'require',
            "title" => 'require',
            "content" => 'require',
        ];
        $msg = [
            "name.require" => __l('online_message', 'Name') . __l('index', 'No_empty'),
            "phone.require" => __l('online_message', 'Phone') . __l('index', 'No_empty'),
            "title.require" => __l('online_message', 'Title') . __l('index', 'No_empty'),
            "content.require" => __l('online_message', 'Content') . __l('index', 'No_empty'),
        ];
        $data = [
            "name" => $param['name'],
            "phone" => $param['phone'],
            "title" => $param['title'],
            "content" => $param['content'],
            "official_category_id" => $param['official_category_id'],
            "weigh" => time()
        ];
        //验证规则
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            return json_encode(['code' => 0, 'msg' => $validate->getError()]);
        }
        //增加数据
        $official_list_form =(new OfficialListOnlineMessage())->create($data);
        if ($official_list_form) {
            return json_encode(['code' => 1, 'msg' => __l('index', 'Submit successfully')]);
        }
    }
}